{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "from hello import say_hello\n",
    "say_hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpaisdk.notebook import parse_notebook_path, get_notebook_path\n",
    "from openpaisdk.core import get_defaults, randstr\n",
    "from openpaisdk.io_utils import to_screen\n",
    "\n",
    "cluster = {\n",
    "    \"cluster_alias\": get_defaults()[\"cluster-alias\"],\n",
    "    \"virtual_cluster\": None,\n",
    "    \"workspace\": get_defaults()[\"workspace\"],\n",
    "}\n",
    "\n",
    "job_name = parse_notebook_path()[0] + '_' + randstr().hex\n",
    "\n",
    "to_screen(cluster)\n",
    "to_screen(job_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpaisdk.core import Job\n",
    "help(Job.from_notebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "__nb_ext_custom_cfg__ = {\n",
    "    \"token\": \"abcdef\", # not to set a int string like 1234\n",
    "    \"image\": 'ufoym/deepo:pytorch-py36-cu90',\n",
    "    \"resources\": {\n",
    "        \"cpu\": 4, \"memoryMB\": 8192, \"gpu\": 0,\n",
    "    },\n",
    "    \"sources\": [\"hello.py\"], \n",
    "    \"pip_installs\": [],\n",
    "}\n",
    "\n",
    "job = Job(job_name).from_notebook(nb_file=get_notebook_path(), cluster=cluster, **__nb_ext_custom_cfg__)\n",
    "# to_screen(job.get_config())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help(Job.submit)\n",
    "job.submit(cluster[\"cluster_alias\"], cluster[\"virtual_cluster\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# restore the job from a name and cluster\n",
    "job2 = Job(job_name).load(cluster_alias=cluster[\"cluster_alias\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# one time check, return {state:..., notebook:...}\n",
    "job2.connect_jupyter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# wait until notebook url is ready\n",
    "help(Job.wait)\n",
    "job2.wait(timeout=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# to_screen(job2.logs()[\"stderr\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# job2.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}